package com.weiiew.versionservice.service;

import com.weiiew.common.entity.dto.version;
import com.weiiew.versionservice.mapper.VersionMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 魏锴
 * @description 针对表【version】的数据库操作Service实现
 * @createDate 2024-02-05 00:31:11
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class VersionServiceImpl implements VersionService {
    private final VersionMapper versionMapper;

    @Override
    @Cacheable(cacheNames = "version:list", key = "1")
    public List<version> getVersionList() {
        return versionMapper.getVersions();
    }

    @Override
    @CacheEvict(cacheNames = "version:list", key = "1")
    public boolean saveOrUpdate(version version) {
        if (version.getId() == null) return versionMapper.create(version);
        else return versionMapper.update(version);
    }

    @Override
    @CacheEvict(cacheNames = "version:list", key = "1")
    public boolean remove(Integer id) {
        if (versionMapper.hasRelation(id)) return false;
        return versionMapper.remove(id);
    }
}




